/**
 * this file will be loaded before server started
 * you can define global functions used in controllers, models, templates
 */

/**
 * use global.xxx to define global functions
 *
 * global.fn1 = function(){
 *     
 * }
 */


"use strict";
var crypto = require('crypto');
//import RoomLifeAdmin from '../../business/room_life.js';
// var global ={};
/**
 * 判断是否是一个合法的时间
 * @param args
 * @returns {boolean}
 */
global.isGoodDate = function (args) {

    if (!args)
        return false;

    if (new Date(args).toString() == "Invalid Date") {
        return false;
    }
    return true;
};


global.md5 = function (text) {
    return crypto.createHash('md5').update(text, "utf-8").digest('hex');
};

/**
 * hmac加密f
 * @param text
 * @returns {*}
 */
global.hmac = function (text) {
    var key = "#$%^TYfGH&^%$#$%EHcFD(&^&f23f";
    return crypto.createHmac('md5', key).update(text, "utf-8").digest('hex');
};

global.msgID = 1;
global.getMsgID = function () {
    return global.msgID++;
};

/**
 * sha1加密， 并大写输出
 * @param text
 * @returns {string}
 */
global.sha1 = function (text) {
    var shasum = crypto.createHash('sha1');
    shasum.update(text);
    return shasum.digest('hex').toUpperCase();
};

//把字符串去掉前后空格
global.trim = function (obj) {
    if (typeof obj == "string") {
        return obj.trim();
    }
    return obj;
};

//得到今天0点的的Date
global.getZeroTD = function () {
    var now = new Date();

    now.setMilliseconds(0);
    now.setHours(0);
    now.setMinutes(0);
    now.setSeconds(0);
    return now;
};

global.replaceAll = function (str, s, d) {

    if (typeof str == "string") {
        return str.split(s).join(d);
    }
    return str;


};

/**
 * 秒转化成天时分秒
 * @param second_time
 * @returns {string}
 */
global.timeStamp = function (second_time) {

    var time = parseInt(second_time) + "秒";
    if (parseInt(second_time) > 60) {

        var second = parseInt(second_time) % 60;
        var min = parseInt(second_time / 60);
        time = min + "分" + second + "秒";

        if (min > 60) {
            min = parseInt(second_time / 60) % 60;
            var hour = parseInt(parseInt(second_time / 60) / 60);
            time = hour + "小时" + min + "分" + second + "秒";

            if (hour > 24) {
                hour = parseInt(parseInt(second_time / 60) / 60) % 24;
                var day = parseInt(parseInt(parseInt(second_time / 60) / 60) / 24);
                time = day + "天" + hour + "小时" + min + "分" + second + "秒";
            }
        }


    }

    return time;
};

//前端节点
global.centerNodeUrlPre = "https://frontxyz." + DOMAIN + ":19997/";

//require("../../business/wx_helper");
// console.log(global.hmac("5rf$R31dYTRD!4!@E"));